package com.example.port.Controller;

import com.example.port.entity.Client;
import com.example.port.service.ClientService;
import com.example.port.dto.ApiResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/clients")
@Tag(name = "客户管理", description = "客户信息管理相关接口")
public class ClientController {
    
    private final ClientService clientService;
    
    @Autowired
    public ClientController(ClientService clientService) {
        this.clientService = clientService;
    }
    
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    @Operation(summary = "创建新客户", description = "添加一个新的客户信息到系统")
    @ApiResponses({
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "201", description = "创建成功"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public ApiResponse<Integer> createClient(@RequestBody Client client) {
        int result = clientService.insertClient(client);
        if (result > 0) {
            return ApiResponse.success(result);
        }
        return ApiResponse.error(500, "创建客户失败");
    }
    
    @GetMapping
    @Operation(summary = "获取所有客户", description = "获取系统中所有客户的信息列表")
    @ApiResponses({
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "获取成功")
    })
    public ApiResponse<List<Client>> getAllClients() {
        List<Client> clients = clientService.getAllClients();
        return ApiResponse.success(clients);
    }
    
    @GetMapping("/{clientId}")
    @Operation(summary = "获取指定客户", description = "根据客户ID获取特定客户的详细信息")
    public ApiResponse<Client> getClientById(@PathVariable Integer clientId) {
        Client client = clientService.getClientById(clientId);
        if (client != null) {
            return ApiResponse.success(client);
        }
        return ApiResponse.error(404, "未找到指定客户");
    }
    
    @PutMapping("/{clientId}")
    @Operation(summary = "更新客户信息", description = "根据客户ID更新特定客户的信息")
    public ApiResponse<Integer> updateClient(@PathVariable Integer clientId, @RequestBody Client client) {
        client.setClientId(clientId);
        int result = clientService.updateClient(client);
        if (result > 0) {
            return ApiResponse.success(result);
        }
        return ApiResponse.error(500, "更新客户信息失败");
    }
    
    @DeleteMapping("/{clientId}")
    @Operation(summary = "删除客户", description = "根据客户ID删除特定客户的信息")
    @ApiResponses({
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "删除成功"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public ApiResponse<Integer> deleteClient(@PathVariable Integer clientId) {
        int result = clientService.deleteClient(clientId);
        if (result > 0) {
            return ApiResponse.success(result);
        }
        return ApiResponse.error(500, "删除客户失败");
    }
}
